﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETA.Entity;
using ETA.IRepository;
using NHibernate;
using NHibernate.Criterion;

namespace ETA.Repositories
{
    public class JoinApplyFormRepository : NhRepositoryBase<JoinApplyForm, string>, IJoinApplyFormRepository
    {
        public IList<JoinApplyForm> GetJoinApplyFormByFilter(JoinApplyForm joinApplyForm, DateTime? Sdate, DateTime? Edate, int Page, int PageSize, out int count)
        {
            count = 0;
            ISession session = NHibernateHelper.GetSession();
            ICriteria crt = session.CreateCriteria(typeof(JoinApplyForm));
            if (joinApplyForm.JoinApplyType != null && joinApplyForm.JoinApplyType.Value>0)
            {
                crt.Add(Restrictions.Eq("JoinApplyType",joinApplyForm.JoinApplyType));
            }

            if (!string.IsNullOrEmpty(joinApplyForm.CompanyType))
            {
                crt.Add(Restrictions.Eq("CompanyType", joinApplyForm.CompanyType));
            }

            if (!string.IsNullOrEmpty(joinApplyForm.AddressProvice))
            {
                crt.Add(Restrictions.Eq("AddressProvice", joinApplyForm.AddressProvice));
            }

            if (!string.IsNullOrEmpty(joinApplyForm.AddressCity))
            {
                crt.Add(Restrictions.Eq("AddressCity", joinApplyForm.AddressCity));
            }

            if (!string.IsNullOrEmpty(joinApplyForm.AddressZone))
            {
                crt.Add(Restrictions.Eq("AddressZone", joinApplyForm.AddressZone));
            }

            if (!string.IsNullOrEmpty(joinApplyForm.AddressSuffix))
            {
                crt.Add(Restrictions.Eq("AddressSuffix", joinApplyForm.AddressSuffix));
            }

            if (!string.IsNullOrEmpty(joinApplyForm.CompanyName))
            {
                crt.Add(Restrictions.Like("CompanyName","%"+joinApplyForm.CompanyName+"%"));
            }

            if (joinApplyForm.HandlerStatus!=null)
            {
                crt.Add(Restrictions.Eq("HandlerStatus", joinApplyForm.HandlerStatus));
            }

            if (joinApplyForm.JoinApplyFormId != null)
            {
                crt.Add(Restrictions.Eq("JoinApplyFormId", joinApplyForm.JoinApplyFormId));
            }

            if (Sdate != null)
            {
                crt.Add(Restrictions.Ge("CreateDate", Sdate));
            }

            if (Edate != null)
            {
                crt.Add(Restrictions.Le("CreateDate", Edate));
            }

            var list = crt.List<JoinApplyForm>();
            count = list.Count;
            list.Skip((Page - 1) * PageSize).Take(PageSize).ToList();
            return list;
        }
    }
}
